package br.indt.framework.threadpool;


public class ThreadPool {
	
	private int maxSize = 0;
	private ThreadPoolWorker[] threadQueue = null;
	
	public ThreadPool(PriorityQueue priorityQueue, int maxSize) {
		this.maxSize = maxSize;
		threadQueue = new ThreadPoolWorker[maxSize];
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < maxSize; i++) {
			sb.append("WorkerThread-").append(i);
			threadQueue[i] = new ThreadPoolWorker(priorityQueue, sb.toString());
			sb.delete(0, sb.length());
		}
	}
	
	public ThreadPoolTask getTask(int taskId) {
		for( int i = 0; i < maxSize; i++) {
			ThreadPoolTask task = threadQueue[i].getCurrentTask();
			if(task != null && task.getId() == taskId) {
				return task;
			}
		}
		return null;
	}
	
	public void cancelCurrentTask() {
		for (int i = 0; i < maxSize; i++) {
			ThreadPoolTask task = threadQueue[i].getCurrentTask();
			if (task != null) {
				task.cancel();
			}
		}
	}
	
	public void stop() {
		for(int i = 0; i < maxSize; i++) {
			threadQueue[i].stop();
		}
	}
}
